<template>
  <view class="container">
    <!--顶部个人信息栏-->
    <view class="header-section">
      <view class="flex padding justify-between">
        <view class="flex align-center">
          <view v-if="!avatar" class="cu-avatar xl round bg-white">
            <view class="iconfont icon-people text-gray icon"></view>
          </view>
          <image v-if="avatar" @click="handleToAvatar" :src="avatar" class="cu-avatar xl round" mode="widthFix">
          </image>
          <view v-if="!name" @click="handleToLogin" class="login-tip">
            点击登录
          </view>
          <view v-if="name" @click="handleToInfo" class="user-info">
            <view class="u_title">
              用户名：{{ name }}
            </view>
          </view>
        </view>
        <view @click="handleToInfo" class="flex align-center">
          <text>个人信息</text>
          <view class="iconfont icon-right"></view>
        </view>
      </view>
    </view>

    <scroll-view class="content-section" scroll-y="true">
      <view class="menu-list">
       <view  class="list-cell list-cell-arrow" @click="handleToSetting">
         <view class="menu-item-box">
           <view class="iconfont icon-setting menu-icon"></view>
           <view>应用设置</view>
         </view>
       </view>
        <view v-if="hasRole('admin') || hasRole('guanliceng')" class="list-cell list-cell-arrow" @click="handleBanqianTotal">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>督查班前报告汇总</view>
          </view>
        </view>
        <view v-if="hasRole('admin') || hasRole('guanliceng')" class="list-cell list-cell-arrow" @click="handleBanhouTotal">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>督查班后报告汇总</view>
          </view>
        </view>
        <view v-if="hasRole('admin') || hasRole('duchayuan')" class="list-cell list-cell-arrow" @click="handleBanqian">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>我的班前工作报告</view>
          </view>
        </view>
        <view v-if="hasRole('admin') || hasAnyRole(['duchayuan', 'guanliceng'])"  class="list-cell list-cell-arrow" @click="handleBanhou">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>我的班后工作报告</view>
          </view>
        </view>
        <view v-if="hasRole('admin') || hasAnyRole(['dj', 'yewuyuan', 'fuwuyuan'])" class="list-cell list-cell-arrow" @click="handleHelp">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>我的奖励</view>
          </view>
        </view>
        <view v-if="hasRole('admin') || hasAnyRole(['dj', 'yewuyuan','fuwuyuan'])" class="list-cell list-cell-arrow" @click="handleAbout">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>我的业绩</view>
          </view>
        </view>
        <view v-if="hasRole('admin') || hasAnyRole(['dj', 'yewuyuan','yewujinli','fuwuyuan','duchayuan','guanliceng'])" class="list-cell list-cell-arrow" @click="handleShangban">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>我的相关房间信息</view>
          </view>
        </view>
 <!--       <view v-if="hasRole('admin') || hasAnyRole(['dj', 'yewuyuan','yewujinli'])" class="list-cell list-cell-arrow" @click="handleTask">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>我的任务</view>
          </view>
        </view> -->
        <view v-if="hasRole('admin') || hasAnyRole(['dj', 'yewuyuan'])" class="list-cell list-cell-arrow" @click="handleQrcode">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>我的二维码</view>
          </view>
        </view>
		<!-- <view v-if="hasRole('admin') || hasAnyRole([ 'fataiyuan'])" class="list-cell list-cell-arrow" @click="handleFatai">
		  <view class="menu-item-box">
		    <view class="iconfont icon-aixin menu-icon"></view>
		    <view>发台信息</view>
		  </view>
		</view> -->
	<!-- 	<view v-if="hasRole('admin') || hasAnyRole([ 'yewujinli','bayuan','guanliceng'])" class="list-cell list-cell-arrow" @click="handleCunjiu">
		  <view class="menu-item-box">
		    <view class="iconfont icon-aixin menu-icon"></view>
		    <view>我的存酒</view>
		  </view>
		</view> -->
		<view v-if="hasRole('admin') || hasAnyRole([ 'bayuan','yewujinli','guanliceng'])" class="list-cell list-cell-arrow" @click="handleJiejiu">
		  <view class="menu-item-box">
		    <view class="iconfont icon-aixin menu-icon"></view>
		    <view>我的借酒</view>
		  </view>
		</view>
    <view v-if="hasRole('admin') || hasAnyRole([ 'yewujinli','bayuan','duchayuan','dj','guanliceng','fuwuyuan','yewuyuan','tongjiyuan'])" class="list-cell list-cell-arrow" @click="handleKaoqin">
		  <view class="menu-item-box">
		    <view class="iconfont icon-aixin menu-icon"></view>
		    <view>我的考勤</view>
		  </view>
		</view>
        <view v-if="hasRole('admin') || hasAnyRole([ 'kaoqinyuan'])" class="list-cell list-cell-arrow" @click="handleKaoqinQrcode">
        	  <view class="menu-item-box">
        	    <view class="iconfont icon-aixin menu-icon"></view>
        	    <view>考勤打卡二维码</view>
        	  </view>
        	</view>
		
        <view v-if="hasRole('admin') || hasAnyRole([ 'fahaoyuan'])" class="list-cell list-cell-arrow" @click="handleFaMa">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>给业务员扫的二维码</view>
          </view>
        </view>
		<view v-if="hasRole('admin') || hasAnyRole([ 'tongjiyuan'])" class="list-cell list-cell-arrow" @click="handleTongJi">
		  <view class="menu-item-box">
		    <view class="iconfont icon-aixin menu-icon"></view>
		    <view>统计录入信息</view>
		  </view>
		</view>
		<view v-if="hasRole('admin') || hasAnyRole([ 'tongjiyuan'])" class="list-cell list-cell-arrow" @click="handleUpTwo">
		  <view class="menu-item-box">
		    <view class="iconfont icon-aixin menu-icon"></view>
		    <view>查看二班人员房间信息</view>
		  </view>
		</view>
       <view  class="list-cell list-cell-arrow" @click="handleTuiDengLu">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>退出登录</view>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
  import { getToken } from '@/utils/auth'
  import { getUserProfile } from '@/api/system/user'

  export default {
    data() {
      return {
        name: this.$store.state.user.name,
        version: getApp().globalData.config.appInfo.version,
        userInfo: {},
        roles: [], // 存储  用 户  角 色
      }
    },
    computed: {
      avatar() {
        return this.$store.state.user.avatar
      },
      windowHeight() {
        return uni.getSystemInfoSync().windowHeight - 50
      },
      userRoleNames() {
        // 这里可以将角色key映射为中文名称，如果需要的话
        return this.roles.join(', ');
      }
    },
    onLoad() {
      this.getUserInfo();
    },
    methods: {
      handleToInfo() {
        this.$tab.navigateTo('/pages/mine/info/index')
      },
      handleToEditInfo() {
        this.$tab.navigateTo('/pages/mine/info/edit')
      },
      handleToSetting() {
        this.$tab.navigateTo('/pages/mine/setting/index')
      },
      handleToLogin() {
        this.$tab.navigateTo('/pages/login')
      },
      handleToAvatar() {
        this.$tab.navigateTo('/pages/mine/avatar/index')
      },
      handleHelp() {
        this.$tab.navigateTo('/pages/mine/help/index')
      },
      handleAbout() {
        this.$tab.navigateTo('/pages/mine/about/index')
      },
      handleShangban() {
        this.$tab.navigateTo('/pages/mine/shangban/shangban')
      },
      handleTask() {
        this.$tab.navigateTo('/pages/mine/task/task')
      },
      handleQrcode() {
        this.$tab.navigateTo('/pages/mine/qrcode/qrcode')
      },
	  handleFatai() {
	    this.$tab.navigateTo('/pages/mine/fataiMsg/fataiMsg')
	  },
      handleFaMa() {
        this.$tab.navigateTo('/pages/mine/fama/fama')
      },
	  handleCunjiu() {
	    this.$tab.navigateTo('/pages/mine/cunjiu/cunjiu')
	  },
	  handleJiejiu() {
	    this.$tab.navigateTo('/pages/mine/jiejiu/jiejiu')
	  },
      handleKaoqin() {
        this.$tab.navigateTo('/pages/mine/kaoqin/kaoqin')
      },
      handleKaoqinQrcode() {
        this.$tab.navigateTo('/pages/mine/kaoqinqrcode/kaoqinqrcode')
      },
      handleUpTwo() {
        this.$tab.navigateTo('/pages/mine/erBanYeWuYuan/erBanYeWuYuan')
      },
      
      handleBanqian() {
        this.$tab.navigateTo('/pages/mine/banqian/banqian')
      },
      handleBanhou() {
        this.$tab.navigateTo('/pages/mine/banhou/banhou')
      },
	  handleTongJi() {
	    this.$tab.navigateTo('/pages/mine/tongji/tongji')
	  },
      handleBanqianTotal() {
        this.$tab.navigateTo('/pages/mine/banqianTotal/banqianTotal')
      },
      handleBanhouTotal() {
        this.$tab.navigateTo('/pages/mine/banhouTotal/banhouTotal')
      },
      handleJiaoLiuQun() {
        this.$modal.showToast('QQ群：①133713780(满)、②146013835(满)、③189091635')
      },
      handleTuiDengLu() {
        this.$modal.confirm('确定注销并退出系统吗？').then(() => {
          this.$store.dispatch('LogOut').then(() => {
            // 清除用户信息
            this.$store.commit('SET_USER', null)
            this.$store.commit('SET_TOKEN', '')
            this.$store.commit('SET_ROLES', [])
            this.$store.commit('SET_PERMISSIONS', [])
            
            // 清除记住的用户名和密码
            uni.removeStorageSync('rememberedUsername')
            uni.removeStorageSync('rememberedPassword')
            uni.removeStorageSync('rememberMe')
            
            // 清除用户信息和角色
            uni.removeStorageSync('userInfo')
            uni.removeStorageSync('roles')
          }).finally(() => {
            // 跳转到登录页面
            this.$tab.reLaunch('/pages/login')
          })
        })
      },
      handleBuilding() {
        this.$modal.showToast('模块建设中~')
      },
      // 获取用户信息
      getUserInfo() {
        const userInfo = uni.getStorageSync('userInfo');
        if (userInfo) {
          this.userInfo = userInfo;
          this.roles = userInfo.roles || []; // 从存储中获取角色
        } else if (getToken()) {
          getUserProfile().then(res => {
            this.userInfo = res.data;
            this.roles = res.data.roles || []; // 从接口获取角色
            uni.setStorageSync('userInfo', res.data);
            uni.setStorageSync('roles', res.data.roles || []);
          });
        }
      },
      // 检查用户是否包含某个角色
      hasRole(role) {
        return this.roles.includes(role);
      },
      // 检查用户是否包含任一指定角色
      hasAnyRole(roles) {
        return roles.some(role => this.roles.includes(role));
      },
    }
  }
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	height: 100vh;
	background-color: #f5f7fa;
}

.header-section {
	flex-shrink: 0; // 防止header被压缩
}

.content-section {
	flex: 1;
	height: 0; // 关键：让scroll-view占据剩余空间
	overflow: hidden;
}

.menu-list {
	padding: 20rpx;
	background-color: #fff;
	border-radius: 16rpx;
	margin: 20rpx;
}

  page {
    background-color: #f5f6f7;
  }

  .mine-container {
    width: 100%;
    height: 100%;

    .header-section {
      padding: 15px 15px 45px 15px;
      background-color: #3c96f3;
      color: white;

      .login-tip {
        font-size: 18px;
        margin-left: 10px;
      }

      .cu-avatar {
        border: 2px solid #eaeaea;

        .icon {
          font-size: 40px;
        }
      }

      .user-info {
        margin-left: 15px;

        .u_title {
          font-size: 18px;
          line-height: 30px;
        }
      }
    }

    .content-section {
      position: relative;
      top: -50px;

      .mine-actions {
        margin: 15px 15px;
        padding: 20px 0px;
        border-radius: 8px;
        background-color: white;

        .action-item {
          .icon {
            font-size: 28px;
          }

          .text {
            display: block;
            font-size: 13px;
            margin: 8px 0px;
          }
        }
      }
    }
  }
</style>
